<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
    >
  <van-cell v-for="(item,index) in list" :key="index" :title="item.title" />
</van-list>
  </div>
</template>

<script>
import { getSearchResults } from '@/api/search/suggestion'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      error: false
    }
  },
  props: {
    search_txt: {
      type: String,
      required: true
    }
  },
  created () {

  },

  methods: {
    async onLoad () {
      // 异步更新数据
      try {
        const { data: res } = await getSearchResults({
          page: this.page,
          per_page: 10,
          q: this.search_txt
        })
        this.list.push(...res.data.results)
        this.page++
        // 数据全部加载完成
        if (this.list.length >= res.total_count) {
          this.finished = true
        }
      } catch (err) {
        this.$toast.fail('获取搜索结果失败！')
        this.error = true
      }
      // 加载状态结束
      this.loading = false
    }
  }
}
</script>

<style scoped lang='less'>
// ::v-deep .van-list {
//   overflow-y: auto;
// }
</style>
